package com.pignest.hotel.servlet.report;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.pignest.hotel.util.DateUtil;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;

public abstract class BaseReport {
	public static final String KEY_STARTTIME = "startTime";
	public static final String KEY_ENDTIME = "endTime";
	
	public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm";
	public static final String DATE_FORMAT = "yyyy-MM-dd";
	
	protected Date startTime;
	protected Date endTime;
	
	private String reportType;
	
	public String getTimeFormat() {
		return TIME_FORMAT;
	}

	public void initParameters(HttpServletRequest req) {
		String timeFormat = getTimeFormat();
		startTime = DateUtil.toDate(req.getParameter(KEY_STARTTIME), timeFormat);
		endTime = DateUtil.toDate(req.getParameter(KEY_ENDTIME), timeFormat);
	}
	
	public abstract String getReportName();
	
	public String getJasperPath() {
		if (reportType != null)
			return "modules/report/jasper/" + reportType + ".jasper";
		return null;
	}
	
	public Map<String, Object> getParameters(HttpServletRequest req) {
		return null;
	}
	
	/*
	 * new JRBeanCollectionDataSource(new ArrayList<String>());
	 * new JREmptyDataSource()
	 */
	public JRDataSource getDataSource(HttpServletRequest req) {
		return new JREmptyDataSource();
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}
	
	

	public String getReportType() {
		return reportType;
	}
	
	public void setReportType(String reportType) {
		this.reportType = reportType;
	}
	
	void notNull(Object o) {
		if (o == null)
			throw new IllegalArgumentException();
	}
	
	void assertTrue(boolean b) {
		if (!b)
			throw new IllegalArgumentException();
	}
	
	void notEmpty(String str) {
		if (StringUtils.isEmpty(str))
			throw new IllegalArgumentException();
	}
}
